import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //m1();
        //m2();
        //m3();
        //m4();
        m5();
    }
//所有
private static void m1() {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    //1.加载驱动
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        //创建连接
        String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
        connection = DriverManager.getConnection(url, "root", "Yan411328");
        if (connection != null) {
            System.out.println("连接成功");
        }
        //2.写sql
        String sql = "select * from user";

        //预编译sql
        preparedStatement = connection.prepareStatement(sql);

        //3.执行sql
        resultSet = preparedStatement.executeQuery();

        //4.遍历结果
        while (resultSet.next()) {
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String pass = resultSet.getString(3);
            System.out.println(id + " " + name + " " + pass);
        }

    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {

        try {
            if (resultSet != null) {
                resultSet.close();
            }

            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (connection != null) {
                connection.close();
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }



        //关闭资源


    }


}
//查询
private static void m2() {
    //键盘录入
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入用户名：");
    String name = scanner.nextLine();


    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    //1.加载驱动
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        //创建连接
        String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
        connection = DriverManager.getConnection(url, "root", "Yan411328");
        if (connection != null) {
            System.out.println("连接成功");
        }
        //2.写sql
        String sql = "select * from user where name like ? ";

        //预编译sql
        preparedStatement = connection.prepareStatement(sql);


        //设置参数
        preparedStatement.setString(1, "%" + name + "%");

        //3.执行sql
        resultSet = preparedStatement.executeQuery();

        //4.遍历结果
        while (resultSet.next()) {
            int id = resultSet.getInt(1);
            String username = resultSet.getString(2);
            String pass = resultSet.getString(3);
            System.out.println(id + " " + username + " " + pass);
        }

    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {

        try {
            if (resultSet != null) {
                resultSet.close();
            }

            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (connection != null) {
                connection.close();
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


        //关闭资源


    }
}
//添加
private static void m3() {
        Scanner scanner = new Scanner(System.in);
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //创建连接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            connection = DriverManager.getConnection(url, "root", "Yan411328");
            if (connection != null) {
                System.out.println("连接成功");
            }
            //2.写sql
            String sql = "insert into user(name,pass) values (?,?)";

            //预编译sql
            preparedStatement = connection.prepareStatement(sql);
            //设置参数
            String a = scanner.next();
            String b = scanner.next();
            preparedStatement.setString(1, a);
            preparedStatement.setString(2, b);

            //3.执行sql
            int result = preparedStatement.executeUpdate();

            //4.遍历结果
            if(result>0){
                System.out.println("插入成功");}
            else{
                System.out.println("插入失败");}


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {

            try {


                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }


            } catch (SQLException e) {
                throw new RuntimeException(e);
            }



            //关闭资源


        }


    }
//修改
private static void m4() {
        Scanner scanner = new Scanner(System.in);
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //创建连接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            connection = DriverManager.getConnection(url, "root", "Yan411328");
            if (connection != null) {
                System.out.println("连接成功");
            }
            //2.写sql
            String sql = "update user set name = ?,pass = ? where id = ?";

            //预编译sql
            preparedStatement = connection.prepareStatement(sql);
            //设置参数
            String a = scanner.next();
            String b = scanner.next();
            preparedStatement.setString(1, a);
            preparedStatement.setString(2, b);
            preparedStatement.setInt(3,Integer.parseInt(scanner.next()));

            //3.执行sql
            int result = preparedStatement.executeUpdate();

            //4.遍历结果
            if(result>0){
                System.out.println("修改成功");}
            else{
                System.out.println("修改失败");}


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {

            try {


                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }


            } catch (SQLException e) {
                throw new RuntimeException(e);
            }



            //关闭资源


        }


    }
//删除
private static void m5() {
        Scanner scanner = new Scanner(System.in);
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //创建连接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            connection = DriverManager.getConnection(url, "root", "Yan411328");
            if (connection != null) {
                System.out.println("连接成功");
            }
            //2.写sql
            String sql = "delete from user where id = ?";

            //预编译sql
            preparedStatement = connection.prepareStatement(sql);
            //设置参数
            preparedStatement.setInt(1,Integer.parseInt(scanner.next()));

            //3.执行sql
            int result = preparedStatement.executeUpdate();

            //4.遍历结果
            if(result>0){
                System.out.println("删除成功");}
            else{
                System.out.println("删除失败");}


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {

            try {


                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }


            } catch (SQLException e) {
                throw new RuntimeException(e);
            }



            //关闭资源


        }


    }

}